package edu.sdbi.judge.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import edu.sdbi.judge.domain.entity.Comment;
import edu.sdbi.judge.domain.vo.StudentResult;
import edu.sdbi.judge.mapper.CommentMapper;
import edu.sdbi.judge.service.GetJudgeIpResultService;
import edu.sdbi.judge.util.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查看评论
 *
 * @author 孙阔
 */
@Order(7)
@Service
public class Judge7 implements GetJudgeIpResultService {
    @Autowired
    private HttpUtil httpUtil;
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public StudentResult getJudgeIpResult(String ip) {
        StudentResult studentResult = new StudentResult();
        JSONObject jsonResponse1 = null;
        JSONObject jsonResponse2 = null;
        double score = 0;
        String url = "http://" + ip + "/comment/list";
        Map<String, String> params = new HashMap<>();
        {
            int commentType = 0;
            String relationId = "1001";
            int page = 1;
            int size = 3;
            params.put("commentType", Integer.toString(commentType));
            params.put("relationId", relationId);
            params.put("page", Integer.toString(page));
            params.put("size", Integer.toString(size));
            try {
                String resultStr = httpUtil.doGet(url, null, params);
                studentResult.setResult("test1:" + resultStr);
                jsonResponse1 = JSONObject.parseObject(resultStr);
                if (jsonResponse1.getInteger("code") == 200) {
                    long l = commentMapper.countCommentsByTypeAndRelation(commentType, relationId);
                    Long total = jsonResponse1.getLong("total");
                    if (total == l) {
                        List<Comment> commentsByTypeAndRelation = commentMapper.getCommentsByTypeAndRelation(
                                commentType, relationId, size * (page - 1), size);
                        JSONArray rows = jsonResponse1.getJSONArray("rows");
                        boolean right = true;
                        for (int i = 0; i < rows.size(); i++) {
                            Comment comment = rows.getObject(i, Comment.class);
                            Comment comment1 = commentsByTypeAndRelation.get(i);
                            if (!comment.equals(comment1)) {
                                right = false;
                            }
                        }
                        if (right) {
                            score += 2;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        {
            int commentType = 1;
            String relationId = "c1";
            int page = 1;
            int size = 3;
            params.put("commentType", Integer.toString(commentType));
            params.put("relationId", relationId);
            params.put("page", Integer.toString(page));
            params.put("size", Integer.toString(size));
            try {
                String resultStr = httpUtil.doGet(url, null, params);
                studentResult.setResult("test1-2:" + resultStr);
                jsonResponse2 = JSONObject.parseObject(resultStr);
                if (jsonResponse2.getInteger("code") == 200) {
                    long l = commentMapper.countCommentsByTypeAndRelation(commentType, relationId);
                    Long total = jsonResponse2.getLong("total");
                    if (total == l) {
                        List<Comment> commentsByTypeAndRelation = commentMapper.getCommentsByTypeAndRelation(
                                commentType, relationId, size * (page - 1), size);
                        JSONArray rows = jsonResponse2.getJSONArray("rows");
                        boolean right = true;
                        for (int i = 0; i < rows.size(); i++) {
                            Comment comment = rows.getObject(i, Comment.class);
                            Comment comment1 = commentsByTypeAndRelation.get(i);
                            if (!comment.equals(comment1)) {
                                right = false;
                            }
                        }
                        if (right) {
                            score += 2;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (jsonResponse1 != null && jsonResponse2 != null) {
            studentResult.setResult(jsonResponse1.toString() + jsonResponse2);
        }
        studentResult.setScore(score);
        studentResult.setItemNumber("7");
        return studentResult;
    }
}
